package com.wjj.happytime.activity.listen_activity;

    import android.support.design.widget.TabLayout;
    import android.support.v4.view.ViewPager;
    import android.support.v7.app.ActionBar;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.Toolbar;
    import android.test.mock.MockApplication;
    import android.view.MenuItem;
    import android.widget.ImageView;
    import com.wjj.happytime.R;
    import com.wjj.happytime.adapter.listen_frg_adapter.ProvinceRadioVPAdapter;
    import com.wjj.happytime.application.BaseApplication;
    import com.wjj.happytime.entity.listen_entity.ListenProvince;
    import com.wjj.happytime.entity.listen_entity.ProvinceDetail;
    import com.wjj.happytime.fragment.listen_frg.ProvinceRadioFrg;
    import com.wjj.happytime.my_interface.HttpService;
    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.List;
    import retrofit2.Call;
    import retrofit2.Callback;
    import retrofit2.Response;

public class ProvinceRadioActivity extends AppCompatActivity implements Callback<ListenProvince> {

  public Toolbar toolBar;
  public TabLayout tabLayout;
//  public ImageView ivMore;
  public ViewPager viewPager;
  public ActionBar actionBar;
  public HttpService service;
  public ArrayList<String> tabNames;
  public LinkedList<ProvinceRadioFrg> frgs;
  public ProvinceRadioVPAdapter adapter;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_province_radio);
    initView();
    initData();
    initEvent();
  }

  private void initEvent() {

  }

  private void initData() {
    //tabLayout和viewPager关联
    tabLayout.setupWithViewPager(viewPager);
    service = ((BaseApplication) getApplication()).getService();
    service.getProvinceData().enqueue(this);
  }

  private void initView() {
    toolBar = ((Toolbar) findViewById(R.id.toolbar_province));
    tabLayout = ((TabLayout) findViewById(R.id.tab_province_radio));
//    ivMore = ((ImageView) findViewById(R.id.iv_provinceradio_arraw_down));
    viewPager = ((ViewPager) findViewById(R.id.viewpager_live_provincepagesecond_id));

    setSupportActionBar(toolBar);
    actionBar = getSupportActionBar();
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayShowHomeEnabled(true);
    actionBar.setDisplayHomeAsUpEnabled(true);
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        onBackPressed();
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override
  public void onResponse(Call<ListenProvince> call, Response<ListenProvince> response) {
    List<ProvinceDetail> result = response.body().getResult();
    //标签名集合
    tabNames = new ArrayList<>();
    for (int i = 0; i < result.size(); i++) {
      tabNames.add(result.get(i).getProvinceName());
    }
    //根据集合是数量来创建Fragment并加入到集合中
    frgs = new LinkedList<>();
    for (int i = 0; i < tabNames.size(); i++) {
      frgs.add(new ProvinceRadioFrg(result.get(i).getProvinceCode()));
    }
    adapter = new ProvinceRadioVPAdapter(getSupportFragmentManager(),tabNames,frgs);
    viewPager.setAdapter(adapter);

  }

  @Override
  public void onFailure(Call<ListenProvince> call, Throwable t) {

  }
}
